<template>
  <!--我的答辩组-->
  <div class="reply-group reply-arrange">
    <InnerBreadcrumb v-model="indexKey" :path="breadPath" :showBack="true" />

    <div class="edu-page-warp" v-show="indexKey === 'MAIN'">
      <!--查看老师/学生详情弹框-->
      <TeacherStuInfoDialog :viewDetailData="viewDetailData" :dialogVisible="dialogVisible" :viewType="viewType" @taacher-stu-close="closeDialog"></TeacherStuInfoDialog>

      <condition-card @reset="reset" @search="searchList" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.graduationYear')">
              <el-date-picker class="search-item" :clearable="false" v-model="queryForm.graduationYear" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.adminCollege')">
              <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
                <el-option v-for="item in source.studentColleges" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.major')">
              <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
                <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('graduationDesign.replyGroup')">
              <el-input v-model="queryForm.defenseGroupName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">{{$t('graduationDesign.replyGroupList')}}</div>
        <div class="edu-opertion-bar">
          <span class="table-prompt">{{$t('graduationDesign.pleaseThirtyMinutesArriveReplySite')}}</span>
        </div>
        <edu-table ref="myReplyGroupTable" :url="getList" @selection-change="handleSelectChange">
          <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="graduationYear" min-width="80"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroup')" prop="defenseGroupName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroupStudent')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row, 'STU')">{{scope.row.defenseGroupStudentNum}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyLeader')" prop="defenseGroupLeaderName" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyGroupTeacher')" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="onDetail(scope.row, 'TEACHER')">{{scope.row.defenseGroupTeacherNum}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyLocation')" prop="defenseLocation" min-width="100"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.replyTime')" prop="defenseStartTime" min-width="110"></edu-table-column>
          <edu-table-column :label="$t('graduationDesign.operation')" min-width="120">
            <template slot-scope="scope">
              <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
              <el-button type="text" @click="onEntry(scope.row)" v-if="scope.row.isDefenseLeader && !scope.row.isSubmit">{{$t('graduationDesign.entryResult')}}</el-button>
              <el-button type="text" v-if="scope.row.isSubmit" disabled>{{$t('graduationDesign.entered')}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
      </el-card>
    </div>
    <div v-if="indexKey === 'ENTRY' || indexKey === 'VIEW'">
      <!--查看/录入成绩-->
      <ReplyStuResultInfo :actionType="actionType" :replyGroupForm="replyStuInfoForm" @reply-result-submit="onSubmit" @reply-result-cancel="onCancel" @reply-result-save="onSubmit"></ReplyStuResultInfo>
    </div>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/assignReplyStudent";
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'myReplyGroup',
  data() {
    return {
      api: API,
      getList: '/api/designservice/defenseGroup/queryMyDefenseGroup',
      queryForm: {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        defenseGroupName: ''
      },
      indexKey: 'MAIN',
      source: {
        studentColleges: [], // 行政学院
        collegeOptions: [], // 学院
        subjectOptions: [] // 专业
      },
      resultSelectedData: [], // 勾选的列表数据
      breadPath: {
        label: this.$t('graduationDesign.myReplyGroup'),
        key: "MAIN",
        children: [
          {
            key: "VIEW",
            label: this.$t('graduationDesign.view')
          },
          {
            key: "ENTRY",
            label: this.$t('graduationDesign.entryResult')
          }
        ]
      },
      loading: false,
      viewType: '', // 老师或学生
      actionType: '',
      viewDetailData: [],
      dialogVisible: false,
      replyStuInfoForm: {} //答辩学生信息
    };
  },
  watch: {
    indexKey: function (newVal, oldVal) {
      this.$emit("reply-bread", newVal);
    }
  },
  mounted() {
    this.queryForm.graduationYear = new Date().getFullYear() + '';
    this.getCollege();
    this.searchList();
    tools.initAllCollege(this.source.studentColleges);
  },
  methods: {
    searchList() {
      this.$refs.myReplyGroupTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        defenseGroupName: ''
      };
      this.source.subjectOptions = [];
      this.queryForm.graduationYear = new Date().getFullYear() + '';
      this.searchList();
    },
    handleSelectChange(val) {
      this.selectedData = val;
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 根据毕业年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.graduationYear) {
        const params = {
          grade: this.queryForm.graduationYear,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 查看答辩组老师 学生
    onDetail(row, type) {
      this.viewType = type;
      let roleType = type === 'TEACHER' ? '0' : '1';
      this.api.getTeacherStuInfo(row.id, roleType).then(res => {
        this.viewDetailData = res.data;
        this.dialogVisible = true;
      }).catch();
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 查看
    onView(row) {
      this.replyStuInfoForm = { ...row };
      this.actionType = "VIEW";
      this.indexKey = "VIEW";
    },
    // 录入成绩
    onEntry(row) {
      this.replyStuInfoForm = { ...row };
      this.actionType = "ENTRY";
      this.indexKey = "ENTRY";
    },
    // 录入成绩保存和提交
    onSubmit(data = {}, operation = "") {
      this.api.save(data).then(res => {
        this.$message.success(operation + this.$t('graduationDesign.success'));
        this.indexKey = "MAIN";
        this.searchList();
      });
    },
    onCancel() {
      this.indexKey = "MAIN";
    }
  },
  components: {
    ReplyStuResultInfo: () => import('./replyStuResultInfo'),
    TeacherStuInfoDialog: () => import('../../assignReplyStudent/components/teacherStuInfoDialog')
  }
};
</script>
<style lang="scss" scoped>
.reply-group {
    .search-item {
        width: 100%;
    }
    .table-prompt {
        position: relative;
        left: 250px;
        top: -24px;
        font-weight: bold;
        color: #ff0000;
    }
}
</style>
